package com.inwk.valoepi.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.inwk.valoepi.managers.PageManager;

/*
 * Abstract class representation of a Page in the UI. Page object pattern
 * 
 */
public abstract class Page {

	protected WebDriver webDriver;
	protected WebDriverWait wait;
	protected PageManager pm;
	protected String baseURL;

	public Page(PageManager pm) {
		this.pm = pm;
		this.webDriver = pm.getWebDriver();
		this.wait = new WebDriverWait(webDriver, 15);
		this.baseURL = pm.getBaseURL();
	}

	public WebDriver getWebDriver() {
		return webDriver;
	}
	
	public WebDriverWait getWebDriverWait() {
		return wait;
	}	

	public String getTitle() {
		return webDriver.getTitle();
	}
	
	public abstract boolean isCurrentPage();
	
	protected void ensurePageLoaded(String xpathExpression) {
		wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpathExpression)));
	}
	
	protected boolean isElementPresent(String xpathExpression) {
		try {
			wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpathExpression)));
			return true;
		} catch (TimeoutException e) {
			return false;
		}
	}

}
